﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;

using System.Xml.Serialization;
using System.IO;
using System.Xml;

namespace HCSExtensions
{
    public static class StringManager
    {
        private static readonly Random random = new Random();
        private static readonly object syncLock = new object();
        public static int GetRandomNumber(int min, int max)
        {
            lock (syncLock)
            { 
                return random.Next(min, max);
            }
        }

        public static string ToHourString(this DateTime date)
        {
            return date.ToString("HH:mm");
        }

        public static string ToDateString(this DateTime date)
        {
            return date.ToString("dd.MM.yyyy");
        }

        public static string FormatString(this string value, params object[] parameters)
        {
            if (parameters.Length == 0)
            {
                return value;
            }

            return string.Format(value, parameters);
        }

       
        public static T DeserializeObject<T>(this string xml)
        {
            XmlSerializer xs = new XmlSerializer(typeof(T));
            MemoryStream memoryStream = new MemoryStream(GenericManager.StringToUTF8ByteArray(xml));
            XmlTextWriter xmlTextWriter = new XmlTextWriter(memoryStream, Encoding.UTF8);
            return (T)xs.Deserialize(memoryStream);
        }

        public static DateTime ParseDateTime(this string date)
        {
            string dayOfWeek = date.Substring(0, 3).Trim();
            string month = date.Substring(4, 3).Trim();
            string dayInMonth = date.Substring(8, 2).Trim();
            string time = date.Substring(11, 9).Trim();
            string offset = date.Substring(20, 5).Trim();
            string year = date.Substring(25, 5).Trim();
            string dateTime = string.Format("{0}-{1}-{2} {3}", dayInMonth, month, year, time);
            DateTime ret = DateTime.Parse(dateTime);
            return ret;
        }

        public static bool IsEqual(this string firstString, string secondString)
        {
            if (string.IsNullOrEmpty(firstString) || string.IsNullOrEmpty(secondString))
            {
                return false;
            }
            
            return firstString.ToLower().Equals(secondString.ToLower());
        }
    }
}
